﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;


public class SpriteListItem
{
    public int sort;
    public Sprite tmpSprite;
}
public class SpriteAtlasFrame
{
    static Dictionary<string, Dictionary<string, SpriteListItem[]>> _cacheKeySprite;// <合图中动作名,精灵合集> key 为图集
    static Dictionary<string, SpriteListItem[]> _cacheSprite;// <合图中动作名,精灵合集> 
    static Dictionary<string, UnityEngine.Object> _cacheAtlas;// <合图路径,合图缓存> 
    static SpriteAtlasFrame instance = null;
    private static ResLoader m_resLoader;
    public static SpriteAtlasFrame Instance
    {
        get {
            if (instance == null)
            {
                instance = new SpriteAtlasFrame();
                _cacheSprite = new Dictionary<string, SpriteListItem[]>();
                _cacheAtlas = new Dictionary<string, UnityEngine.Object>();
                _cacheKeySprite = new Dictionary<string, Dictionary<string, SpriteListItem[]>>();
            }
            return instance;
        }
    }

    public void getSpriteAtlas(string atlasPath, string atlasKey,  string spriteName, Action<SpriteListItem[]> callback)
    {
        SpriteListItem[] priteListItem = null;
        if (!_cacheAtlas.ContainsKey(atlasPath))
        {
            // 加载
            if (m_resLoader == null)
                m_resLoader = ResLoader.Alloc();

            m_resLoader.Add2LoadRapid(atlasPath, (success, name, asset) =>
            {
                // if (_cacheSprite.ContainsKey(spriteName))
                // {
                //     priteListItem = _cacheSprite[spriteName];
                //     callback?.Invoke(priteListItem);
                //     return;
                // }

                if (!success)
                {
                    callback?.Invoke(null);
                    return;
                }

                _cacheAtlas[atlasPath] = asset as UnityEngine.Object;
                object[] allSubAssets = UMTResource.GetAllSubAssets(asset);
                if (allSubAssets == null)
                {
                    callback?.Invoke(null);
                    return;
                }

                // 解析
                if (allSubAssets.Length == 0)
                {
                    callback?.Invoke(null);
                    return;
                }

                var _cacheSpriteList = new Dictionary<string, List<SpriteListItem>>();
                var _cacheSpriteI = new Dictionary<string, SpriteListItem[]>();
                List<SpriteListItem> spriteList = null;
                string spriteSplitName = null;
                //防止第一个类型错误为Texture2D
                int startIndex = 0;
                if (allSubAssets[0] is Texture2D)
                {
                    startIndex = 1;
                }

                for (int i = startIndex; i < allSubAssets.Length; i++)
                {
                    Sprite tmpSprite = allSubAssets[i] as Sprite;
                    SpriteListItem tmpSpriteListItem = new SpriteListItem();
                    if (tmpSprite)
                    {
                        string[] strs = tmpSprite.name.Split('-');
                        if (spriteSplitName == null ||
                            !tmpSprite.name.Contains(spriteSplitName)) // 当前拆分的动画名发生改变
                        {
                            spriteSplitName = "";
                            for (int j = 0; j < strs.Length-1; j++)
                            {
                                if (j == 0)
                                {
                                    spriteSplitName = spriteSplitName + strs[j];
                                }
                                else
                                {
                                    spriteSplitName = spriteSplitName + "-" + strs[j];
                                }
                                
                            }
                            // spriteSplitName = strs[0];
                            // 取出需要插入的list
                            if (_cacheSpriteList.ContainsKey(spriteSplitName))
                            {
                                spriteList = _cacheSpriteList[spriteSplitName];
                            }
                            else
                            {
                                spriteList = new List<SpriteListItem>();
                                _cacheSpriteList[spriteSplitName] = spriteList;
                            }

                        }

                        tmpSpriteListItem.sort = int.Parse((strs[strs.Length - 1]));
                        tmpSpriteListItem.tmpSprite = tmpSprite;
                        // tmpSprite.name = strs[strs.Length - 1];
                        spriteList.Add(tmpSpriteListItem);
                    }
                }

                // 排序
                foreach (var key in _cacheSpriteList.Keys)
                {
                    List<SpriteListItem> list = _cacheSpriteList[key];
                    list.Sort((sprite, sprite1) =>
                    {
                        var t1 = sprite.sort;
                        var t2 = sprite1.sort;
                        return t1 - t2;
                    });
                    _cacheSpriteI[key] = list.ToArray();
                }

                _cacheKeySprite[atlasKey] = _cacheSpriteI;
                
                if (_cacheKeySprite.ContainsKey(atlasKey))
                {
                    if (_cacheKeySprite[atlasKey].ContainsKey(spriteName))
                    {
                        priteListItem = _cacheKeySprite[atlasKey][spriteName];
                        callback?.Invoke(priteListItem);
                    }
                }
                else
                {
                    callback?.Invoke(null);
                }
            });
            m_resLoader.Load();
            return;
        }
        else
        {
            if (_cacheKeySprite.ContainsKey(atlasKey))
            {
                if (_cacheKeySprite[atlasKey].ContainsKey(spriteName))
                {
                    priteListItem = _cacheKeySprite[atlasKey][spriteName];
                    callback?.Invoke(priteListItem);
                    return;
                }
            }
        }

        callback?.Invoke(null);
    }

    public void disposeRes()
    {
        _cacheSprite = null;
        _cacheAtlas = null;
        _cacheKeySprite = null;
        instance = null;
        if (m_resLoader != null)
        {
            m_resLoader.Recycle2Cache();
            m_resLoader = null;
        }
    }
}
